package com.zhilei.controller;

import com.zhilei.base.BaseInfoProperties;
import com.zhilei.canal.DataDictSyncHelper;
import com.zhilei.grace.result.GraceJSONResult;
import com.zhilei.pojo.DataDictionary;
import com.zhilei.pojo.bo.DataDictionaryBO;
import com.zhilei.pojo.bo.QueryDictItemsBO;
import com.zhilei.pojo.vo.CompanyPointsVO;
import com.zhilei.pojo.vo.DataDictionaryVO;
import com.zhilei.service.DataDictionaryService;
import com.zhilei.thread.MyThreadPool;
import com.zhilei.untils.GsonUtils;
import com.zhilei.untils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 创建行业类别
 *
 * @author 志磊
 * @serial 2024-09-26
 */
@Slf4j
@RestController
@RequestMapping("dataDict")
public class DataDictController extends BaseInfoProperties {

    @Autowired
    private DataDictionaryService dataDictionaryService;

    @Autowired
    private MyThreadPool myThreadPool;

    /**
     * 创建数据字典
     *
     * @param dataDictionaryBO
     * @return
     */
    @PostMapping("create")
    public GraceJSONResult create(@RequestBody DataDictionaryBO dataDictionaryBO) {
        dataDictionaryService.createDataDict(dataDictionaryBO);
        return GraceJSONResult.ok();
    }

    /**
     * 删除数据字典
     *
     * @param dictId
     * @return
     */
    @PostMapping("delete")
    public GraceJSONResult delete(@RequestParam String dictId) {
        dataDictionaryService.deleteDataDictById(dictId);
        return GraceJSONResult.ok();
    }

    /**
     * 修改数据字典
     *
     * @param dataDictionaryBO
     * @return
     */
    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody DataDictionaryBO dataDictionaryBO) {
        dataDictionaryService.modifyDataDict(dataDictionaryBO);
        return GraceJSONResult.ok();
    }

    /**
     * 查询需要修改的数据字典
     *
     * @param dictId
     * @return
     */
    @PostMapping("item")
    public GraceJSONResult item(@RequestParam("dictId") String dictId) {
        DataDictionaryVO dataDictionaryVOItem = dataDictionaryService.getModifyDataDictById(dictId);
        return GraceJSONResult.ok(dataDictionaryVOItem);
    }

    /**
     * 查询数据字典，实现分页查询
     *
     * @param typeName
     * @param itemValue
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("list")
    public GraceJSONResult list(@RequestParam("typeName") String typeName,
                                @RequestParam("itemValue") String itemValue,
                                @RequestParam("page") Integer page,
                                @RequestParam("limit") Integer limit) {

        if (page == null) {
            page = 1;
        }

        if (limit == null) {
            limit = 10;
        }

        PagedGridResult dataDictVOList = dataDictionaryService.getDataDictList(typeName, itemValue, page, limit);
        return GraceJSONResult.ok(dataDictVOList);
    }

    /**
     * 根据类型编码查询数据字典
     *
     * @param typeCode
     * @return
     */
    @PostMapping("app/getDataByCode")
    public GraceJSONResult getDataByCode(@RequestParam("typeCode") String typeCode) {

        if (StringUtils.isBlank(typeCode)) {
            return GraceJSONResult.error();
        }

        String ddKey = DataDictSyncHelper.DD_KEY + typeCode;
        String ddPrefix = redis.get(ddKey);

        // TODO 初始化list列表
        List<DataDictionary> dataDictionarieList = null;
        // TODO 校验redis中是否存在，如果不存在就设置一个空list,存在就在存在的基础上进行更新
        if (StringUtils.isNotBlank(ddPrefix)) {
            // TODO 如果存在就将存在的数据转换成list
            dataDictionarieList = GsonUtils.stringToListAnother(ddPrefix, DataDictionary.class);
        }

        return GraceJSONResult.ok(dataDictionarieList);
    }

    /**
     * 获得数据字典
     *
     * @param itemsBO
     * @return
     */
    @PostMapping("app/getItemsByKeys")
    public GraceJSONResult getItemsByKeys(@RequestBody QueryDictItemsBO itemsBO) {

        //        // TODO 公司优势
//        String[] advantage = itemsBO.getAdvantage();
//        // TODO 福利待遇
//        String[] benefits = itemsBO.getBenefits();
//        // TODO 薪资福利
//        String[] bonus = itemsBO.getBonus();
//        // TODO 补助津贴
//        String[] subsidy = itemsBO.getSubsidy();
//
//        // TODO 公司优势的key
//        List<DataDictionary> advantageByKeys = dataDictionaryService.getItemsByKeys(advantage);
//        // TODO 福利待遇
//        List<DataDictionary> benefitsByKeys = dataDictionaryService.getItemsByKeys(benefits);
//        // TODO 薪资福利
//        List<DataDictionary> bonusByKeys = dataDictionaryService.getItemsByKeys(bonus);
//        // TODO 补助津贴
//        List<DataDictionary> subsidyByKeys = dataDictionaryService.getItemsByKeys(subsidy);

        ThreadPoolExecutor threadPoolExecutor = myThreadPool.threadPoolExecutor();
        CompanyPointsVO list = new CompanyPointsVO();

        // TODO 使用线程池异步的方式进行请求
        CompletableFuture<List<DataDictionary>> advantageFuture = CompletableFuture.supplyAsync(() -> {
            String[] advantage = itemsBO.getAdvantage();
            List<DataDictionary> advantageByKeys = dataDictionaryService.getItemsByKeys(advantage);
            list.setAdvantageList(advantageByKeys);
            return advantageByKeys;
        }, threadPoolExecutor);

        CompletableFuture<List<DataDictionary>> benefitsFuture = CompletableFuture.supplyAsync(() -> {
            String[] benefits = itemsBO.getBenefits();
            List<DataDictionary> benefitsByKeys = dataDictionaryService.getItemsByKeys(benefits);
            list.setBenefitsList(benefitsByKeys);
            return benefitsByKeys;
        }, threadPoolExecutor);

        CompletableFuture<List<DataDictionary>> bonusFuture = CompletableFuture.supplyAsync(() -> {
            String[] bonus = itemsBO.getBonus();
            List<DataDictionary> bonusByKeys = dataDictionaryService.getItemsByKeys(bonus);
            list.setBonusList(bonusByKeys);
            return bonusByKeys;
        }, threadPoolExecutor);

        CompletableFuture<List<DataDictionary>> subsidyFuture = CompletableFuture.supplyAsync(() -> {
            String[] subsidy = itemsBO.getSubsidy();
            List<DataDictionary> subsidyByKeys = dataDictionaryService.getItemsByKeys(subsidy);
            list.setSubsidyList(subsidyByKeys);
            return subsidyByKeys;
        }, threadPoolExecutor);

        CompletableFuture<Void> allOFuture = CompletableFuture.allOf(advantageFuture, benefitsFuture, bonusFuture, subsidyFuture);
        try {
            allOFuture.get();
        } catch (Exception e) {
            GraceJSONResult.errorMsg("线程获取失败！");
        }
        return GraceJSONResult.ok(list);
    }
}












































